package com.yyt.module_main.vm;

import android.databinding.ObservableField;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.vondear.rxtool.RxRegTool;
import com.yyt.libsrc.bean.LoginRegisterBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.MsgHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;

import top.lixb.libcommon.binding.BindingViewModel;

public class RegisterVM extends BindingViewModel {
    public ObservableField<String> account = new ObservableField<>();
    public ObservableField<String> authcode = new ObservableField<>();
    public ObservableField<String> pwd1 = new ObservableField<>();
    public ObservableField<String> pwd2 = new ObservableField<>();
    public ObservableField<String> inviteCode = new ObservableField<>();

    /**
     * 注册
     * @param view
     */
    public void registNow(View view) {
        if (!RxRegTool.isMobileSimple(account.get())) {
            MsgHelper.showToast("手机号码不正确");
            return;
        }
        if (!TextUtils.isEmpty(pwd1.get()) && !TextUtils.equals(pwd1.get(), pwd2.get())) {
            MsgHelper.showToast("两次输入的密码不一致");
            return;
        }
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.login_register)
                .addP("phone",account.get())
                .addP("authCode",authcode.get())
                .addP("loginPwd",pwd1.get())
                .addP("inviteCode",inviteCode.get())
                .build(), new SuccessCallback<LoginRegisterBean>() {
            @Override
            public void onSuccess(LoginRegisterBean loginRegisterBean) {
                MsgHelper.showToast("注册成功");
                Bundle bundle = new Bundle();
                bundle.putString("mobile", authcode.get());
                bundle.putString("pwd", pwd1.get());
                RouterHelper.getInstance().startAct(RouteTable.main_login,bundle);
                finish();
            }
        });
    }


    /**
     * 清空输入
     * @param view
     */
    public void clearInput(View view) {
        account.set("");
    }

}
